#include "GameSceneBank.h"

IGameSceneBank* GameSceneBank::_instance = NULL;

GameSceneBank::GameSceneBank(void)
{
}


GameSceneBank::~GameSceneBank(void)
{
}

void GameSceneBank::addSceneFactory( string name, IGameSceneFactory* sceneFactory )
{
	if (sceneFactory == NULL)
		return;

	map<string, IGameSceneFactory*>::iterator item = _listScenceFactory.find(name);
	if (item != _listScenceFactory.end())
	{
		// Exist factory
		_listScenceFactory.erase(name);
	}
	_listScenceFactory[name] = sceneFactory;
}

CCScene* GameSceneBank::getScene( string name )
{
	map<string, IGameSceneFactory*>::iterator iInFactory = _listScenceFactory.find(name);
	if (iInFactory == _listScenceFactory.end()) // Scene is not support
		return NULL;

	// Check if scene exist in _listScene
	map<string, CCScene*>::iterator iInScene = _listScene.find(name);
	if (iInScene == _listScene.end()) // If not exist
	{
		_listScene[name] = _listScenceFactory[name]->createGameScene();
	}

	return _listScene[name];
}

CCScene* GameSceneBank::getNewScene( string name )
{
	map<string, IGameSceneFactory*>::iterator iInFactory = _listScenceFactory.find(name);
	if (iInFactory == _listScenceFactory.end()) // Scene is not support
		return NULL;

	return iInFactory->second->createGameScene();
}

IGameSceneBank* GameSceneBank::sharedGameSceneBank()
{
	if (_instance == NULL) 
		_instance = new GameSceneBank();

	return _instance;
}


